package com.daykw.wenshu.task;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;

import com.daykw.wenshu.config.NutzDaoConfig;
import com.daykw.wenshu.model.CaseText;
import com.daykw.wenshu.model.SearchSubDailyUrl;
import com.daykw.wenshu.utils.SpringContextHolder;

public class RestJob extends CustmTask {

	private static Logger logger = Logger.getLogger(RestJob.class);

	public void doStart() {

		this.doWork();

	}

	@Override
	public void doWork() {

		try {
			NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);

			List<Record> ds = dao.query("t_wenshu_search_daily_url", Cnd.wrap("total <= 10000 and total >2000 "));
			for (Record r : ds) {
				String start = r.getString("start_date");
				String city = r.getString("city");
				String court_level = r.getString("court_level");

				dao.update(SearchSubDailyUrl.class, Chain.make("status", 0), Cnd.where("start_date", "=", start)
						.and("city", "=", city).and("court_level", "=", court_level).and("status", "<>", 2));

			}

			logger.info("完成子列表" + ds.size() + "状态修改");
		} catch (Exception e) {
			logger.error("", e);
		}

	}

	private void oldJob() {

		try {
			NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);

			List<Record> ds = dao.query("t_wenshu_exist_doc_id", Cnd.where("status", "=", "0").limit(1, 10000));
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
			for (Record task : ds) {
				String doc_id = task.getString("doc_id");
				try {
					CaseText caset = new CaseText();
					String date = df.format(new Date());
					caset.setTxt(null);
					caset.setTxt_len(0l);
					caset.setDoc_id(doc_id);
					caset.setIs_deleted("N");
					caset.setSource(1);
					caset.setCreate_date(Integer.parseInt(date));
					dao.fastInsert(caset);

				} catch (Exception e) {
					dao.update(CaseText.class, Chain.make("source", 1), Cnd.where("doc_id", "=", doc_id));
				}
				dao.update("t_wenshu_exist_doc_id", Chain.make("status", 2), Cnd.where("doc_id", "=", doc_id));
			}
			logger.info("修復了" + ds.size() + "文章DOCID");
		} catch (Exception e) {
			logger.error("", e);
		}

	}

}
